<!-- 封装的item 组件-->
<script setup>
import FormComponent from '@/views/mainPage/detail/index.vue'
import {onMounted, ref} from "vue";
import router from "@/router/index.js";

defineProps({
  item: {
    type: Object,
    default: () => { }
  }
})

const showForm = ref(false)
const selectedItem = ref({})
// 获取当前用户
const authDataStr= sessionStorage.getItem('Authentication');
let username = "";
if (authDataStr){
  const authData = JSON.parse(authDataStr);
  username = authData.username;
}

const toggleForm =  (item, username) => {


  if (item && username && username !=="") {

    // 获取当前的物品值
    selectedItem.value = item

    // 显示表单
    showForm.value = !showForm.value
  } else {
    console.log("您无权访问，请登录")
     router.push('/login'); // 跳转到登录页
  }

}

</script>

<template>
  <div class="goods-item" @click="toggleForm(item,username)">
    <img v-ima-lazy="item.image" alt=""  src=""/>
    <p class="name ellipsis">{{ item.itemName }}</p>

  </div>
  <FormComponent v-if="showForm" :item="selectedItem" />
</template>

<style scoped lang="scss">
.goods-item {
  display: block;
  width: 220px;
  padding: 20px 30px;
  text-align: center;
  transition: all .5s;

  &:hover {
    transform: translate3d(0, -3px, 0);
    box-shadow: 0 3px 8px rgb(0 0 0 / 20%);
  }

  img {
    width: 160px;
    height: 160px;
  }

  p {
    padding-top: 10px;
  }

  .name {
    font-size: 16px;
  }

  .desc {
    color: #999;
    height: 29px;
  }

  .price {
    color: $priceColor;
    font-size: 20px;
  }
}
</style>